<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class XcRenterFee extends Model
{
    protected $table = 'xc_renter_fee';
    protected $guarded = ['id'];

    
    /**
     * 
     * 获得指定年或者月份费用的数组
     * 
     */
    public static function GetFeeTotal($date, $renter_id)
    {
        $len = mb_strlen($date);
        if ($len == 4){//取一年的数据
            $res = self::whereRaw("SUBSTRING(ym,1,4) = '$date'");
        }else if ($len == 7){//取某个月的数据
            $res = self::whereRaw("SUBSTRING(ym,1,7) = '$date'");
        }
        //如果限定场地的话
        if ($renter_id > 0){
            $res = $res->where("renter_id", $renter_id);
        }

        $res = $res->get()->toArray();
        $data =[];
        foreach ($res as $key => $value) {
            if (!isset($data[$value['renter_id']])){
                $data[$value['renter_id']] = [
                        'renter_fee' => $value['renter_fee'], //场租费用
                        'property_fee' => $value['property_fee'], //物业费用
                        'water_fee' => $value['water_fee'], //水费
                        'electric_fee' => $value['electric_fee'], //电费
                        'public_relations_fee' => $value['public_relations_fee']//公关费用
                        ];
            }else{
                $data[$value['renter_id']]['renter_fee']+= $value['renter_fee'];
                $data[$value['renter_id']]['property_fee']+= $value['property_fee'];
                $data[$value['renter_id']]['water_fee']+= $value['water_fee'];
                $data[$value['renter_id']]['electric_fee']+= $value['electric_fee'];
                $data[$value['renter_id']]['public_relations_fee']+= $value['public_relations_fee'];
            }
            
        }
        return $data;

    }
}
